package org.crumbleworks.crumbchat.client.controller;

import java.io.IOException;

import org.crumbleworks.crumbchat.client.ChatClient;
import org.crumbleworks.crumbchat.client.ClientListener;
import org.crumbleworks.crumbchat.client.InputParser;
import org.crumbleworks.crumbchat.common.data.Message;
import org.crumbleworks.crumbchat.common.data.UserData;

public class ClientController implements ClientControllerInterface {
    private ChatClient chatClient;
    private ClientListener clientListener;
    private UserData userData;

    private GuiController guiController;
    private ConsoleController consoleController;

    private InputParser inputParser;

    private boolean guiEnabled;
    private boolean consoleEnabled;

    public ClientController(boolean guiEnabled, boolean consoleEnabled) {
        this.guiEnabled = guiEnabled;
        this.consoleEnabled = consoleEnabled;

        clientListener = new ClientListener(this);

        chatClient = new ChatClient();
        chatClient.addListener(clientListener);

        userData = new UserData();
        inputParser = new InputParser(this);

        if (guiEnabled) {
            guiController = new GuiController(this);
        }
        if (consoleEnabled) {
            consoleController = new ConsoleController(this);
        }
    }

    @Override
    public void connectToServer(String host, String username) {
        chatClient.setHost(host);
        userData.setUsername(username);

        try {
            chatClient.connect();
        }
        catch (IOException tempEx) {
            showConnectError(chatClient.getHost());
        }
    }

    @Override
    public void disconnectFromServer(boolean shutdownAfterDisconnect) {
        chatClient.disconnect();

        if (shutdownAfterDisconnect) {
            shutdown();
        }
    }

    @Override
    public void sendMessageToServer(String text) {
        Message message = new Message(userData.getUsername(), text);

        chatClient.sendMessage(message);
        putMessageToChat(message);
    }

    @Override
    public void putMessageToChat(Message message) {
        if (guiEnabled) {
            guiController.putMessageToChat(message);
        }
        if (consoleEnabled) {
            consoleController.putMessageToChat(message);
        }
    }

    @Override
    public void showConnectError(String host) {
        if (guiEnabled) {
            guiController.showConnectError(host);
        }
        if (consoleEnabled) {
            consoleController.showConnectError(host);
        }
    }

    @Override
    public void shutdown() {
        if (guiEnabled) {
            guiController.shutdown();
        }
        if (consoleEnabled) {
            consoleController.shutdown();
        }

        System.exit(0);
    }

    @Override
    public void parseInput(String input) {
        inputParser.parseInput(input);
    }

    public void sendUserDataToServer() {
        chatClient.sendUserData(userData);
    }

    public static void main(String[] args) {
        new ClientController(true, false);
    }
}
